﻿using PerfAnalyzerLib;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using System.Collections.Generic;
using System.Threading;

namespace UnitTests
{
    /// <summary>
    ///This is a test class for FileParserTest and is intended
    ///to contain all FileParserTest Unit Tests
    ///</summary>
	[TestClass()]
	public class FileParserTest
	{
		private TestContext testContextInstance;

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext
		{
			get
			{
				return testContextInstance;
			}
			set
			{
				testContextInstance = value;
			}
		}

		#region Additional test attributes
		// 
		//You can use the following additional attributes as you write your tests:
		//
		//Use ClassInitialize to run code before running the first test in the class
		//[ClassInitialize()]
		//public static void MyClassInitialize(TestContext testContext)
		//{
		//}
		//
		//Use ClassCleanup to run code after all tests in a class have run
		//[ClassCleanup()]
		//public static void MyClassCleanup()
		//{
		//}
		//
		//Use TestInitialize to run code before running each test
		//[TestInitialize()]
		//public void MyTestInitialize()
		//{
		//}
		//
		//Use TestCleanup to run code after each test has run
		//[TestCleanup()]
		//public void MyTestCleanup()
		//{
		//}
		//
		#endregion

		/// <summary>
		/// Test GetLine with one line.
		/// Read the line and end of file.
		/// </summary>
		[TestMethod()]
		public void FileParser_GetLine_OneLineTest()
		{
			string expected = "testString";
			string filename = GeneratorCsvFile.GenerateFile(new string[] { expected });

			FileParser target = new FileParser(filename);
			
			// Test for content
			string actual = target.GetLine();
			Assert.AreEqual(expected, actual);

			// Test for end of file
			actual = target.GetLine();
			Assert.AreEqual(null, actual);

			File.Delete(filename);
		}

		/// <summary>
		/// Test GetLine with 10 lines.
		/// Read the lines and end of file.
		/// </summary>
		[TestMethod()]
		public void FileParser_GetLine_10LinesTest()
		{
			int numberOfLines = 10;
			string expected = "testString";

			List<string> expectedLines = new List<string>();
			for (int i = 0; i < numberOfLines; i++)
			{
				expectedLines.Add(expected + i);
			}

			string filename = GeneratorCsvFile.GenerateFile(expectedLines);

			FileParser target = new FileParser(filename);
			string actual;

			// Test for content
			for (int i = 0; i < numberOfLines; i++)
			{
				actual = target.GetLine();
				Assert.AreEqual(expectedLines[i], actual);
			}

			// Test for end of file
			actual = target.GetLine();
			Assert.AreEqual(null, actual);

			File.Delete(filename);
		}

		/// <summary>
		/// Test the StopPArsing function.
		/// </summary>
		[TestMethod()]
		public void FileParser_StopParsingTest()
		{
			int numberOfLines = 10;
			string expected = "testString";

			List<string> expectedLines = new List<string>();
			for (int i = 0; i < numberOfLines; i++)
			{
				expectedLines.Add(expected + i);
			}

			string filename = GeneratorCsvFile.GenerateFile(expectedLines);

			FileParser target = new FileParser(filename);
			string actual;

			// Test for content
			for (int i = 0; i < (int)(numberOfLines/2); i++)
			{
				actual = target.GetLine();
				Assert.AreEqual(expectedLines[i], actual);
			}

			target.StopParsing();

			// Test for end of file
			actual = target.GetLine();
			Assert.AreEqual(null, actual);

			File.Delete(filename);
		}
	}
}
